﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using BLL;
using Common;
namespace WebManage.Controllers
{
    public class BookController : Controller
    {
        //
        // GET: /Book/
        public ActionResult Index()
        {
            return View();
        }
        public JsonResult query_book(int pageindex, int pagesize)
        {
            try
            {
                BookBLL bll = new BookBLL();
                List<Book> list = bll.GetBookBycondtion(pageindex, pagesize);
                object pagecount = bll.GetBookBycondtionCount();
                return Json(new { MessageCode = "0", BookList = list, Pagecount = (int.Parse(pagecount.ToString()) + pagesize - 1) / pagesize }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);

            }

        }

        public JsonResult GetBook(int id)
        {
            BookBLL bll = new BookBLL();
            Book model = bll.GetModel(id);
            return Json(new { model = model }, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult OpeaBook()
        {

                BookBLL bll = new BookBLL();
                int id = Request["id"] != null ? int.Parse(Request["id"].ToString()) : 0;
                string opea = Request["opea"] != null ? Request["opea"] : "";
                var flag = true;
                string rebook = Request["rebook"] != null ? Request["rebook"] : "";
                Book model = new Book { ID = id,rebook = rebook };
                        flag = bll.updateBook(model);
                    if (flag)
                    {
                        return Json(new { MessageCode = "0" }, JsonRequestBehavior.AllowGet);
                    }
                    else
                    {
                        return Json(new { MessageCode = "100" }, JsonRequestBehavior.AllowGet);
                    }
        }
	}
}